package com.geekbang.work2;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author nhsoft.lul
 */
public class PreparedStatementApplication {

    static String URL = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
    static String USER = "root";
    static String PASSWORD = "123456";

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "delete from book_order";
            statement = conn.prepareStatement(sql);
            statement.execute();
            long start = System.currentTimeMillis();
            conn.setAutoCommit(false);
            sql = "insert into book_order(order_num,customer_num,order_total_money,order_discount_money,create_at,update_at,pay_at) values(?,?,?,?,now(),now(),now())";
            statement = conn.prepareStatement(sql);
            for(int i = 0 ; i < 500 ; i++){
                statement.setInt(1, i + 1);
                statement.setInt(2, i + 1);
                statement.setBigDecimal(3, BigDecimal.ONE);
                statement.setBigDecimal(4, BigDecimal.ONE);
                statement.execute();
            }
            conn.commit();
            conn.setAutoCommit(true);
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end - start));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
